import MinRequest from './service'
import { config } from './config'

const minRequest = new MinRequest()

const { result_code, error_code } = config

// 请求拦截器
minRequest.interceptors.request((request) => {
  if (!request.hiddenLoading) {
    uni.showLoading({
      title: '加载中',
      mask: true
    })
  }
  return request
})

// 响应拦截器
minRequest.interceptors.response((response) => {
  uni.hideLoading()
  if (response && response.data) {
    if (response.data.code === result_code) {
      return response.data
    }
    if (response.data.code === error_code) {
      // 错误码处理
    } else {
      uni.showToast({
        title: response.data.message || response.data.msg,
        icon: 'none'
      })
    }
  }
  return response.data
})

export default minRequest
